<?php
namespace App\Http\Controllers;

use App\Http\Requests\SmsRequest;
use App\Models\ActivityApply;
use App\Models\RoleUser;
use App\Models\SmsTemplate;
use App\Models\User;
use App\Models\UserInvestCategory;
use App\Models\Zone;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Category;

class SmsController extends CommonController
{

    /**
     * 短信发送管理界面
     */
    public function getIndex()
    {
        $lists = SmsTemplate::get();
        $datas = Category::all();
        return view('sms.index')->with('lists', $lists)->with('datas', $datas);
    }

    /**
     * 执行发送短信
     */
    public function postSend(SmsRequest $request)
    {
        $data = $request->input();
//        dd($data);
        $blade = SmsTemplate::where('id', $data['blade'])->first();

        if($blade->related_activity==1 && $data['activity']==''){
            return AjaxCallbackMessage('请填写活动标题', false);
        }

        if($blade->related_project==1 && $data['project']==''){
            return AjaxCallbackMessage('请填写项目标题', false);
        }

        if($data['type']=='other' && $data['phone']==''){
            return AjaxCallbackMessage('请填写手机号码', false);
        }

        $content = str_replace("::project",$data['project'],$blade->content);
        $content = str_replace("::activity",$data['activity'],$content);

        if('all'==$data['type']){
            $usernames = User::lists('username');
        }elseif('investor'==$data['type']){
//            $lists = RoleUser::with('user')->where('role_id', 3)->get();
//            $usernames = array();
//            foreach($lists as $k=>$v){
//                isset($v->user)&& $usernames[]=$v->user->username;
//            }


            if(!isset($data['investIndustry'])|| 0==count($data['investIndustry'])){
                return AjaxCallbackMessage('请选择主投方向', false);
            }

            $iCategory = UserInvestCategory::whereIn('category_id',$data['investIndustry'])->get();
            $uids = array();
            foreach($iCategory as $k=>$v){
                $uids[] = $v->uid;
            }

            $uids = array_flip(array_flip(array_filter($uids)));

            $usernames = User::whereIn('uid', $uids)->lists('username')->toArray();

        }elseif('projector'==$data['type']){
            $lists = RoleUser::with('user')->where('role_id', 2)->get();
            $usernames = array();
            foreach($lists as $k=>$v){
                isset($v->user)&& $usernames[]=$v->user->username;
            }
        }elseif('maker'==$data['type']){
            $lists = RoleUser::with('user')->where('role_id', 4)->get();
            $usernames = array();
            foreach($lists as $k=>$v){
                isset($v->user)&& $usernames[]=$v->user->username;
            }
        }elseif('other'==$data['type']){
            $usernames = explode(',', str_replace('，',',', $data['phone']));
            //剔除可能出现的连续两个逗号
            $usernames = array_filter($usernames, function($v){if($v==''){ return false;} else{ return true;}});

            $usernames = array_map(function($v){return trim($v);}, $usernames);
            foreach($usernames as $k=>$v){
                if(''==$v){
                    unset($usernames[$k]);
                }
            }
        }
        $num = $err = 0;
        foreach($usernames as $k=>$v){
            $res = SendSMS($v,$content,'adminSend',3);
            if($res!=1){
                $err+=1;
//            	return SendSmsErrorMessage($res);
            }else{
                $num+=1;
            }
        }

        return AjaxCallbackMessage('已经成功发送'.$num.'条短信,'.$err.'条短信发送失败了', true);
    }


}